/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution{
public:
  bool hasCycle(ListNode *head){

    ListNode *m = head;
    ListNode *n = head;
    bool f = false;

    while(m != nullptr && n != nullptr){
      m = m->next;
      n = n->next;
      if(n){
        n = n->next;
      }

      if(m == n && m && n){
        f = true;
        break;
      }
    }

    return f;

  }
};